package com.miku.blog.service.impl;

import com.miku.blog.domain.UserAuth;
import com.miku.blog.domain.pojo.LoginUser;
import com.miku.blog.domain.pojo.ResponseResult;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.eume.HttpStatusEnum;
import com.miku.blog.service.EmailService;
import com.miku.blog.service.UserAuthService;
import com.miku.blog.service.UserInfoService;
import com.miku.blog.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author shkstart
 * @create ${}YEAR-06-17 0:39
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private UserAuthService userAuthService;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public ResponseResult sendEmail(String email, Integer sendStatus) {

        if (!EmailUtils.checkEmail(email)) {
            return new ResponseResult(HttpStatusEnum.THE_MAILBOX_IS_MALFORMED);
        }

        String nickName;
        UserAuth userAuth = userAuthService.getUserAuthByEmail(email);
        if (SystemConstance.EMAIL_SEND_TYPE_BY_REGISTER.equals(sendStatus)) {
            //注册:         判断是否已存在相同邮箱的用户
            if (Objects.nonNull(userAuth)) {
                return new ResponseResult(HttpStatusEnum.USER_ALREADY_EXISTS);
            }
            nickName = EmailHtmlTemplateUtils.DEFAULT_USERNAME;
        } else if (SystemConstance.EMAIL_SEND_TYPE_BY_UPDATE_EMAIL.equals(sendStatus)) {
            //更换绑定邮箱:   判断是否与已绑定的邮箱相同 或 新邮箱是否已有用户存在
            LoginUser loginUser = SecurityUtils.getLoginUser();
            UserAuth user = loginUser.getUserAuth();

            if (user.getEmail().equals(email)) {
                return new ResponseResult(HttpStatusEnum.DO_NOT_BIND_THE_SAME_MAILBOX);
            }
            nickName = loginUser.getUserInfo().getNickname();

        } else if (SystemConstance.EMAIL_SEND_TYPE_BY_FORGOT_PASSWORD.equals(sendStatus)) {
            //忘记密码:    判断是否有存在该邮箱对应的用户
            if (Objects.isNull(userAuth)) {
                return new ResponseResult(HttpStatusEnum.ACCOUNT_DOES_NOT_EXIST);
            }
            nickName = userInfoService.getUserInfoByUserId(userAuth.getId()).getNickname();

        } else {
            return new ResponseResult(HttpStatusEnum.PARAM_ERROR);
        }

        Date startTime = new Date();
        Date endTime = new Date(startTime.getTime() + SystemConstance.CODE_VALIDITY_TIME);
        SimpleDateFormat dateFormat = DateUtils.getSimpleDateFormatByCN();

        String code = EmailUtils.sendEmail(javaMailSender, email, nickName, dateFormat.format(startTime), dateFormat.format(endTime));
        if (!StringUtils.hasText(code)) {
            return new ResponseResult(HttpStatusEnum.SEND_EMAIL_ERROR);
        }
        String creatJwt = JwtUtils.creatJwt(email, startTime, endTime, code);
        return ResponseResult.ok().setData(creatJwt).setMessage(HttpStatusEnum.EMAIL_SEND_SUCCESS.getMsg());
    }
}
